package com.kerwin.springcloud.controller;

import com.kerwin.springcloud.service.PaymenHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderHystrixController {

    @Resource
    private PaymenHystrixService paymenHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String payment_ok(@PathVariable("id") Integer id){
        String result = paymenHystrixService.payment_ok(id);
        log.info("#############result:" + result);
        return result;
    }

    @HystrixCommand(fallbackMethod = "payment_timeout_hander", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    public String payment_timeout(@PathVariable("id") Integer id){
//        int x = 10/ 0;
        String result = paymenHystrixService.payment_timeout(id);
        log.info("#############result:" + result);
        return result;
    }

    public String payment_timeout_hander(Integer id){
        return "客户端80降级，线程池：" + Thread.currentThread().getName() + "payment_timeout_hander; id = " + id + ";超时/异常";
    }
}
